#basic definitions for tools
arch := $(if $(arch),$(arch),native)
CXX := $(if $(CXX),$(CXX),g++)
LDD := $(if $(LDD),$(LDD),g++)
AR := $(if $(AR),$(AR),ar)

#set options according to mode
ifeq ($(mode),release)
cxx_mode_flags := -O3 -DNDEBUG
else ifeq ($(mode),debug)
cxx_mode_flags := -O0
else
$(error Invalid mode)
endif

#setup profiling
ifdef profile
cxx_mode_flags += -pg
ld_mode_flags += -pg
endif

#setup PIC code
ifdef pic
cxx_mode_flags += -fPIC
ld_mode_flags += -shared
endif

#setup code coverage
ifdef coverage
cxx_mode_flags += --coverage
ld_mode_flags += --coverage
endif

#setup flags
CXX_FLAGS := $(cxx_mode_flags) $(cxx_flags) -g3 \
	$(addprefix -D, $(definitions)) \
	-march=$(arch) \
	-funroll-loops -funsigned-char -fno-strict-aliasing \
	-W -Wall -ansi -pipe \
	$(addprefix -I, $(include_dirs))

AR_FLAGS := cru
LD_FLAGS := $(ld_mode_flags) $(ld_flags) -pipe

#specify endpoint commands
build_obj_cmd = $(CXX) $(CXX_FLAGS) -c -MMD $< -o $@
build_lib_cmd = $(AR) $(AR_FLAGS) $@ $^
link_cmd = $(LDD) $(LD_FLAGS) -o $@ $(object_files) \
	$(if $(libraries),-L$(libs_dir) $(addprefix -l,$(libraries)),) \
	$(if $(dynamic_libs),-L$(output_dir) $(addprefix -l,$(dynamic_libs)),) \
	$(if $($(platform)_libraries),$(addprefix -l,$($(platform)_libraries)),)

#specify postlink command- generate pdb file
postlink_cmd = objcopy --only-keep-debug $@ $@.pdb && \
	strip $@ && \
	objcopy --add-gnu-debuglink=$@.pdb $@

#include generated dependensies
include $(wildcard $(objects_dir)/*.d)

.PHONY: analyze analyze_all

analyze:
	@echo "Analyzing $(target)" > coverage.log
	@for i in $(source_files);do gcov -lp -o $(objects_dir) $$i >> coverage.log; done
	@echo `pwd`
	@perl $(path_step)/make/compilers/gcc_coverage.pl

analyze_deps: $(depends)

analyze_all: analyze analyze_deps
